home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / ubiquity / install.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  60KB  |  2,018 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import sys
  5. import os
  6. import platform
  7. import errno
  8. import stat
  9. import re
  10. import textwrap
  11. import shutil
  12. import subprocess
  13. import time
  14. import struct
  15. import socket
  16. import fcntl
  17. import traceback
  18. import syslog
  19. import gzip
  20. import debconf
  21. import warnings
  22. warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning)
  23. import apt_pkg
  24. from apt.cache import Cache
  25. from apt.progress import FetchProgress, InstallProgress
  26. from hashlib import md5
  27. sys.path.insert(0, '/usr/lib/ubiquity')
  28. from ubiquity import misc
  29. from ubiquity import osextras
  30. from ubiquity.components import language_apply, apt_setup, timezone_apply, clock_setup, console_setup_apply, usersetup_apply, hw_detect, check_kernels, migrationassistant_apply
  31.  
  32. def debconf_disconnect():
  33.     '''Disconnect from debconf. This is only to be used as a subprocess
  34.     preexec_fn helper.'''
  35.     os.environ['DEBIAN_FRONTEND'] = 'noninteractive'
  36.     if 'DEBIAN_HAS_FRONTEND' in os.environ:
  37.         del os.environ['DEBIAN_HAS_FRONTEND']
  38.     
  39.     if 'DEBCONF_USE_CDEBCONF' in os.environ:
  40.         del os.environ['DEBCONF_USE_CDEBCONF']
  41.     
  42.  
  43.  
  44. class DebconfFetchProgress(FetchProgress):
  45.     """An object that reports apt's fetching progress using debconf."""
  46.     
  47.     def __init__(self, db, title, info_starting, info):
  48.         FetchProgress.__init__(self)
  49.         self.db = db
  50.         self.title = title
  51.         self.info_starting = info_starting
  52.         self.info = info
  53.         self.old_capb = None
  54.         self.eta = 0
  55.  
  56.     
  57.     def start(self):
  58.         self.db.progress('START', 0, 100, self.title)
  59.         if self.info_starting is not None:
  60.             self.db.progress('INFO', self.info_starting)
  61.         
  62.         self.old_capb = self.db.capb()
  63.         capb_list = self.old_capb.split()
  64.         capb_list.append('progresscancel')
  65.         self.db.capb(' '.join(capb_list))
  66.  
  67.     
  68.     def pulse(self):
  69.         FetchProgress.pulse(self)
  70.         
  71.         try:
  72.             self.db.progress('SET', int(self.percent))
  73.         except debconf.DebconfError:
  74.             return False
  75.  
  76.         if self.eta != 0:
  77.             time_str = '%d:%02d' % divmod(int(self.eta), 60)
  78.             self.db.subst(self.info, 'TIME', time_str)
  79.             
  80.             try:
  81.                 self.db.progress('INFO', self.info)
  82.             except debconf.DebconfError:
  83.                 return False
  84.             except:
  85.                 None<EXCEPTION MATCH>debconf.DebconfError
  86.             
  87.  
  88.         None<EXCEPTION MATCH>debconf.DebconfError
  89.         return True
  90.  
  91.     
  92.     def stop(self):
  93.         if self.old_capb is not None:
  94.             self.db.capb(self.old_capb)
  95.             self.old_capb = None
  96.             self.db.progress('STOP')
  97.         
  98.  
  99.  
  100.  
  101. class DebconfInstallProgress(InstallProgress):
  102.     """An object that reports apt's installation progress using debconf."""
  103.     
  104.     def __init__(self, db, title, info, error = None):
  105.         InstallProgress.__init__(self)
  106.         self.db = db
  107.         self.title = title
  108.         self.info = info
  109.         self.error_template = error
  110.         self.started = False
  111.         flags = fcntl.fcntl(self.statusfd.fileno(), fcntl.F_GETFL)
  112.         fcntl.fcntl(self.statusfd.fileno(), fcntl.F_SETFL, flags & ~(os.O_NONBLOCK))
  113.  
  114.     
  115.     def startUpdate(self):
  116.         self.db.progress('START', 0, 100, self.title)
  117.         self.started = True
  118.  
  119.     
  120.     def error(self, pkg, errormsg):
  121.         if self.error_template is not None:
  122.             self.db.subst(self.error_template, 'PACKAGE', pkg)
  123.             self.db.subst(self.error_template, 'MESSAGE', errormsg)
  124.             self.db.input('critical', self.error_template)
  125.             self.db.go()
  126.         
  127.  
  128.     
  129.     def statusChange(self, pkg, percent, status):
  130.         self.percent = percent
  131.         self.status = status
  132.         self.db.progress('SET', int(percent))
  133.         self.db.subst(self.info, 'DESCRIPTION', status)
  134.         self.db.progress('INFO', self.info)
  135.  
  136.     
  137.     def updateInterface(self):
  138.         if self.statusfd is None:
  139.             return False
  140.         
  141.         
  142.         try:
  143.             while not self.read.endswith('\n'):
  144.                 r = os.read(self.statusfd.fileno(), 1)
  145.                 if not r:
  146.                     return False
  147.                 
  148.                 self.read += r
  149.                 continue
  150.                 self
  151.         except OSError:
  152.             (err, errstr) = None
  153.             print errstr
  154.  
  155.         if self.read.endswith('\n'):
  156.             s = self.read
  157.             (status, pkg, percent, status_str) = s.split(':', 3)
  158.             if status == 'pmerror':
  159.                 self.error(pkg, status_str)
  160.             elif status == 'pmconffile':
  161.                 match = re.compile("\\s*'(.*)'\\s*'(.*)'.*").match(status_str)
  162.                 if match:
  163.                     self.conffile(match.group(1), match.group(2))
  164.                 
  165.             else:
  166.                 self.statusChange(pkg, float(percent), status_str.strip())
  167.             self.read = ''
  168.         
  169.         return True
  170.  
  171.     
  172.     def run(self, pm):
  173.         child_pid = self.fork()
  174.         if child_pid == 0:
  175.             os.close(self.writefd)
  176.             
  177.             try:
  178.                 while self.updateInterface():
  179.                     pass
  180.             except (KeyboardInterrupt, SystemExit):
  181.                 pass
  182.             except:
  183.                 for line in traceback.format_exc().split('\n'):
  184.                     syslog.syslog(syslog.LOG_WARNING, line)
  185.                 
  186.  
  187.             os._exit(0)
  188.         
  189.         self.statusfd.close()
  190.         saved_stdin = os.dup(0)
  191.         
  192.         try:
  193.             null = os.open('/dev/null', os.O_RDONLY)
  194.             os.dup2(null, 0)
  195.             os.close(null)
  196.         except OSError:
  197.             pass
  198.  
  199.         saved_stdout = os.dup(1)
  200.         os.dup2(2, 1)
  201.         saved_environ_keys = ('DEBIAN_FRONTEND', 'DEBIAN_HAS_FRONTEND', 'DEBCONF_USE_CDEBCONF')
  202.         saved_environ = { }
  203.         for key in saved_environ_keys:
  204.             if key in os.environ:
  205.                 saved_environ[key] = os.environ[key]
  206.                 continue
  207.         
  208.         os.environ['DEBIAN_FRONTEND'] = 'noninteractive'
  209.         if 'DEBIAN_HAS_FRONTEND' in os.environ:
  210.             del os.environ['DEBIAN_HAS_FRONTEND']
  211.         
  212.         if 'DEBCONF_USE_CDEBCONF' in os.environ:
  213.             del os.environ['DEBCONF_USE_CDEBCONF']
  214.         
  215.         res = pm.ResultFailed
  216.         
  217.         try:
  218.             res = pm.DoInstall(self.writefd)
  219.         finally:
  220.             os.close(self.writefd)
  221.             while True:
  222.                 
  223.                 try:
  224.                     (pid, status) = os.waitpid(child_pid, 0)
  225.                     if pid != child_pid:
  226.                         break
  227.                     
  228.                     if os.WIFEXITED(status) or os.WIFSIGNALED(status):
  229.                         break
  230.                 continue
  231.                 except OSError:
  232.                     break
  233.                     continue
  234.                 
  235.  
  236.                 None<EXCEPTION MATCH>OSError
  237.             os.dup2(saved_stdin, 0)
  238.             os.close(saved_stdin)
  239.             os.dup2(saved_stdout, 1)
  240.             os.close(saved_stdout)
  241.             for key in saved_environ_keys:
  242.                 if key in saved_environ:
  243.                     os.environ[key] = saved_environ[key]
  244.                     continue
  245.                 if key in os.environ:
  246.                     del os.environ[key]
  247.                     continue
  248.             
  249.  
  250.         return res
  251.  
  252.     
  253.     def finishUpdate(self):
  254.         if self.started:
  255.             self.db.progress('STOP')
  256.             self.started = False
  257.         
  258.  
  259.  
  260.  
  261. class InstallStepError(Exception):
  262.     '''Raised when an install step fails.
  263.  
  264.     Attributes:
  265.         message -- message returned with exception
  266.  
  267.     '''
  268.     
  269.     def __init__(self, message):
  270.         Exception.__init__(self, message)
  271.         self.message = message
  272.  
  273.  
  274.  
  275. class Install:
  276.     
  277.     def __init__(self):
  278.         '''Initial attributes.'''
  279.         if os.path.isdir('/rofs'):
  280.             self.source = '/rofs'
  281.         elif os.path.isdir('/UNIONFS'):
  282.             self.source = '/UNIONFS'
  283.         else:
  284.             self.source = '/var/lib/ubiquity/source'
  285.         self.target = '/target'
  286.         self.kernel_version = platform.release()
  287.         self.db = debconf.Debconf()
  288.         self.select_language_packs()
  289.         self.blacklist = { }
  290.         if self.db.get('ubiquity/install/generate-blacklist') == 'true':
  291.             self.db.progress('START', 0, 100, 'ubiquity/install/title')
  292.             self.db.progress('INFO', 'ubiquity/install/blacklist')
  293.             self.generate_blacklist()
  294.         
  295.         apt_pkg.InitConfig()
  296.         apt_pkg.Config.Set('Dir', '/target')
  297.         apt_pkg.Config.Set('Dir::State::status', '/target/var/lib/dpkg/status')
  298.         apt_pkg.Config.Set('APT::GPGV::TrustedKeyring', '/target/etc/apt/trusted.gpg')
  299.         apt_pkg.Config.Set('Acquire::gpgv::Options::', '--ignore-time-conflict')
  300.         apt_pkg.Config.Set('DPkg::Options::', '--root=/target')
  301.         apt_pkg.Config.Clear('DPkg::Pre-Install-Pkgs')
  302.         apt_pkg.InitSystem()
  303.  
  304.     
  305.     def excepthook(self, exctype, excvalue, exctb):
  306.         '''Crash handler. Dump the traceback to a file so that it can be
  307.         read by the caller.'''
  308.         if issubclass(exctype, KeyboardInterrupt) or issubclass(exctype, SystemExit):
  309.             return None
  310.         
  311.         tbtext = ''.join(traceback.format_exception(exctype, excvalue, exctb))
  312.         syslog.syslog(syslog.LOG_ERR, 'Exception during installation:')
  313.         for line in tbtext.split('\n'):
  314.             syslog.syslog(syslog.LOG_ERR, line)
  315.         
  316.         tbfile = open('/var/lib/ubiquity/install.trace', 'w')
  317.         print >>tbfile, tbtext
  318.         tbfile.close()
  319.         sys.exit(1)
  320.  
  321.     
  322.     def run(self):
  323.         '''Run the install stage: copy everything to the target system, then
  324.         configure it as necessary.'''
  325.         self.db.progress('START', 0, 100, 'ubiquity/install/title')
  326.         self.db.progress('INFO', 'ubiquity/install/mounting_source')
  327.         
  328.         try:
  329.             if self.source == '/var/lib/ubiquity/source':
  330.                 self.mount_source()
  331.             
  332.             self.db.progress('SET', 1)
  333.             self.db.progress('REGION', 1, 75)
  334.             
  335.             try:
  336.                 self.copy_all()
  337.             except EnvironmentError:
  338.                 e = None
  339.                 if e.errno in (errno.ENOENT, errno.EIO, errno.EFAULT, errno.ENOTDIR, errno.EROFS):
  340.                     if e.filename is None:
  341.                         error_template = 'cd_hd_fault'
  342.                     elif e.filename.startswith('/target'):
  343.                         error_template = 'hd_fault'
  344.                     else:
  345.                         error_template = 'cd_fault'
  346.                     error_template = 'ubiquity/install/copying_error/%s' % error_template
  347.                     self.db.subst(error_template, 'ERROR', str(e))
  348.                     self.db.input('critical', error_template)
  349.                     self.db.go()
  350.                     sys.exit(3)
  351.                 elif e.errno == errno.ENOSPC:
  352.                     error_template = 'ubiquity/install/copying_error/no_space'
  353.                     self.db.subst(error_template, 'ERROR', str(e))
  354.                     self.db.input('critical', error_template)
  355.                     self.db.go()
  356.                     sys.exit(3)
  357.                 else:
  358.                     raise 
  359.             except:
  360.                 e.errno in (errno.ENOENT, errno.EIO, errno.EFAULT, errno.ENOTDIR, errno.EROFS)
  361.  
  362.             self.db.progress('SET', 75)
  363.             self.db.progress('REGION', 75, 76)
  364.             self.db.progress('INFO', 'ubiquity/install/locales')
  365.             self.configure_locales()
  366.             self.db.progress('SET', 76)
  367.             self.db.progress('REGION', 76, 77)
  368.             self.db.progress('INFO', 'ubiquity/install/user')
  369.             self.configure_user()
  370.             self.db.progress('SET', 77)
  371.             self.db.progress('REGION', 77, 78)
  372.             self.run_target_config_hooks()
  373.             self.db.progress('SET', 78)
  374.             self.db.progress('REGION', 78, 79)
  375.             self.db.progress('INFO', 'ubiquity/install/network')
  376.             self.configure_network()
  377.             self.db.progress('SET', 79)
  378.             self.db.progress('REGION', 79, 80)
  379.             self.db.progress('INFO', 'ubiquity/install/apt')
  380.             self.configure_apt()
  381.             self.db.progress('SET', 80)
  382.             self.db.progress('REGION', 80, 85)
  383.             
  384.             try:
  385.                 self.install_language_packs()
  386.             except InstallStepError:
  387.                 pass
  388.             except IOError:
  389.                 pass
  390.             except SystemError:
  391.                 pass
  392.  
  393.             self.db.progress('SET', 85)
  394.             self.db.progress('REGION', 85, 86)
  395.             self.db.progress('INFO', 'ubiquity/install/timezone')
  396.             self.configure_timezone()
  397.             self.db.progress('SET', 86)
  398.             self.db.progress('REGION', 86, 87)
  399.             self.db.progress('INFO', 'ubiquity/install/keyboard')
  400.             self.configure_keyboard()
  401.             self.db.progress('SET', 87)
  402.             self.db.progress('REGION', 87, 88)
  403.             self.db.progress('INFO', 'ubiquity/install/migrationassistant')
  404.             self.configure_ma()
  405.             self.db.progress('SET', 88)
  406.             self.db.progress('REGION', 88, 89)
  407.             self.remove_unusable_kernels()
  408.             self.db.progress('SET', 89)
  409.             self.db.progress('REGION', 89, 93)
  410.             self.db.progress('INFO', 'ubiquity/install/hardware')
  411.             self.configure_hardware()
  412.             self.db.progress('SET', 93)
  413.             self.db.progress('REGION', 93, 94)
  414.             self.db.progress('INFO', 'ubiquity/install/bootloader')
  415.             self.configure_bootloader()
  416.             self.db.progress('SET', 94)
  417.             self.db.progress('REGION', 94, 95)
  418.             self.db.progress('INFO', 'ubiquity/install/installing')
  419.             self.install_extras()
  420.             self.db.progress('SET', 95)
  421.             self.db.progress('REGION', 95, 99)
  422.             self.db.progress('INFO', 'ubiquity/install/removing')
  423.             self.remove_extras()
  424.             self.remove_broken_cdrom()
  425.             self.db.progress('SET', 99)
  426.             self.db.progress('INFO', 'ubiquity/install/log_files')
  427.             self.copy_logs()
  428.             self.db.progress('SET', 100)
  429.         finally:
  430.             self.cleanup()
  431.             
  432.             try:
  433.                 self.db.progress('STOP')
  434.             except (KeyboardInterrupt, SystemExit):
  435.                 raise 
  436.             except:
  437.                 pass
  438.  
  439.  
  440.  
  441.     
  442.     def copy_file(self, sourcepath, targetpath, md5_check):
  443.         sourcefh = None
  444.         targetfh = None
  445.         
  446.         try:
  447.             while None:
  448.                 sourcefh = open(sourcepath, 'rb')
  449.                 targetfh = open(targetpath, 'wb')
  450.                 if md5_check:
  451.                     sourcehash = md5()
  452.                 
  453.                 while None:
  454.                     buf = sourcefh.read(16384)
  455.                     if not buf:
  456.                         break
  457.                     
  458.                     if md5_check:
  459.                         sourcehash.update(buf)
  460.                         continue
  461.                     continue
  462.                     if not md5_check:
  463.                         break
  464.                     
  465.                 targetfh = open(targetpath, 'rb')
  466.                 if md5_check:
  467.                     targethash = md5()
  468.                 
  469.                 while None:
  470.                     buf = targetfh.read(16384)
  471.                     if not buf:
  472.                         break
  473.                     
  474.                     continue
  475.                     if targethash.digest() != sourcehash.digest():
  476.                         if targetfh:
  477.                             targetfh.close()
  478.                         
  479.                         if sourcefh:
  480.                             sourcefh.close()
  481.                         
  482.                         error_template = 'ubiquity/install/copying_error/md5'
  483.                         self.db.subst(error_template, 'FILE', targetpath)
  484.                         self.db.input('critical', error_template)
  485.                         self.db.go()
  486.                         response = self.db.get(error_template)
  487.                         if response == 'skip':
  488.                             break
  489.                         elif response == 'abort':
  490.                             syslog.syslog(syslog.LOG_ERR, 'MD5 failure on %s' % targetpath)
  491.                             sys.exit(3)
  492.                         elif response == 'retry':
  493.                             pass
  494.                         
  495.                 response == 'skip'
  496.                 break
  497.             if targetfh:
  498.                 targetfh.close()
  499.             
  500.             if sourcefh:
  501.                 sourcefh.close()
  502.             
  503.             return None
  504.  
  505.  
  506.     
  507.     def find_cd_kernel(self):
  508.         '''Find the boot kernel on the CD, if possible.'''
  509.         release_bits = os.uname()[2].split('-')
  510.         if len(release_bits) >= 3:
  511.             subarch = release_bits[2]
  512.         else:
  513.             subarch = None
  514.         for prefix in ('vmlinux', 'vmlinuz'):
  515.             kernel = os.path.join('/cdrom/casper', prefix)
  516.             if os.path.exists(kernel):
  517.                 return kernel
  518.             
  519.             if subarch:
  520.                 kernel = os.path.join('/cdrom/casper', subarch, prefix)
  521.                 if os.path.exists(kernel):
  522.                     return kernel
  523.                 
  524.                 kernel = os.path.join('/cdrom/casper', '%s-%s' % (prefix, subarch))
  525.                 if os.path.exists(kernel):
  526.                     return kernel
  527.                 
  528.             os.path.exists(kernel)
  529.         
  530.  
  531.     
  532.     def generate_blacklist(self):
  533.         if os.path.exists('/cdrom/casper/filesystem.manifest-desktop') and os.path.exists('/cdrom/casper/filesystem.manifest'):
  534.             desktop_packages = set()
  535.             manifest = open('/cdrom/casper/filesystem.manifest-desktop')
  536.             for line in manifest:
  537.                 if line.strip() != '' and not line.startswith('#'):
  538.                     desktop_packages.add(line.split()[0])
  539.                     continue
  540.             
  541.             manifest.close()
  542.             live_packages = set()
  543.             manifest = open('/cdrom/casper/filesystem.manifest')
  544.             for line in manifest:
  545.                 if line.strip() != '' and not line.startswith('#'):
  546.                     live_packages.add(line.split()[0])
  547.                     continue
  548.             
  549.             manifest.close()
  550.             difference = live_packages - desktop_packages
  551.         else:
  552.             difference = set()
  553.         difference -= self.query_recorded_installed()
  554.         archdetect = subprocess.Popen([
  555.             'archdetect'], stdout = subprocess.PIPE)
  556.         subarch = archdetect.communicate()[0].strip()
  557.         if subarch.startswith('amd64/') and subarch.startswith('i386/') or subarch.startswith('lpia/'):
  558.             difference -= set([
  559.                 'grub'])
  560.         elif subarch == 'powerpc/ps3':
  561.             pass
  562.         elif subarch.startswith('powerpc/'):
  563.             difference -= set([
  564.                 'yaboot',
  565.                 'hfsutils'])
  566.         
  567.         if len(difference) == 0:
  568.             self.blacklist = { }
  569.             return None
  570.         
  571.         use_restricted = True
  572.         
  573.         try:
  574.             if self.db.get('apt-setup/restricted') == 'false':
  575.                 use_restricted = False
  576.         except debconf.DebconfError:
  577.             pass
  578.  
  579.         if not use_restricted:
  580.             cache = Cache()
  581.             for pkg in cache.keys():
  582.                 if cache[pkg].isInstalled and cache[pkg].section.startswith('restricted/'):
  583.                     difference.add(pkg)
  584.                     continue
  585.             
  586.             del cache
  587.         
  588.         cache = Cache()
  589.         confirmed_remove = set()
  590.         for pkg in sorted(difference):
  591.             if pkg in confirmed_remove:
  592.                 continue
  593.             
  594.             would_remove = self.get_remove_list(cache, [
  595.                 pkg], recursive = True)
  596.             if would_remove <= difference:
  597.                 confirmed_remove |= would_remove
  598.                 continue
  599.             for removedpkg in would_remove:
  600.                 cachedpkg = self.get_cache_pkg(cache, removedpkg)
  601.                 cachedpkg.markKeep()
  602.             
  603.         
  604.         difference = confirmed_remove
  605.         difference = set(filter((lambda x: not os.path.exists('/var/lib/dpkg/info/%s.prerm' % x)), difference))
  606.         cmd = [
  607.             'dpkg',
  608.             '-L']
  609.         cmd.extend(difference)
  610.         subp = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  611.         res = subp.communicate()[0].splitlines()
  612.         u = { }
  613.         for x in res:
  614.             u[x] = 1
  615.         
  616.         self.blacklist = u
  617.  
  618.     
  619.     def copy_all(self):
  620.         '''Core copy process. This is the most important step of this
  621.         stage. It clones live filesystem into a local partition in the
  622.         selected hard disk.'''
  623.         files = []
  624.         total_size = 0
  625.         self.db.progress('START', 0, 100, 'ubiquity/install/title')
  626.         self.db.progress('INFO', 'ubiquity/install/scanning')
  627.         walklen = 0
  628.         for entry in os.walk(self.source):
  629.             walklen += 1
  630.         
  631.         walkpos = 0
  632.         walkprogress = 0
  633.         for dirpath, dirnames, filenames in os.walk(self.source):
  634.             walkpos += 1
  635.             if int((float(walkpos) / walklen) * 10) != walkprogress:
  636.                 walkprogress = int((float(walkpos) / walklen) * 10)
  637.                 self.db.progress('SET', walkprogress)
  638.             
  639.             sourcepath = dirpath[len(self.source) + 1:]
  640.             for name in dirnames + filenames:
  641.                 relpath = os.path.join(sourcepath, name)
  642.                 fqpath = os.path.join(dirpath, name)
  643.                 if relpath != 'etc/fstab':
  644.                     total_size += os.lstat(fqpath).st_size
  645.                     files.append(relpath)
  646.                     continue
  647.             
  648.         
  649.         self.db.progress('SET', 10)
  650.         self.db.progress('INFO', 'ubiquity/install/copying')
  651.         copy_progress = 0
  652.         (copied_size, counter) = (0, 0)
  653.         directory_times = []
  654.         time_start = time.time()
  655.         times = [
  656.             (time_start, copied_size)]
  657.         long_enough = False
  658.         time_last_update = time_start
  659.         if self.db.get('ubiquity/install/md5_check') == 'false':
  660.             md5_check = False
  661.         else:
  662.             md5_check = True
  663.         old_umask = os.umask(0)
  664.         for path in files:
  665.             sourcepath = os.path.join(self.source, path)
  666.             targetpath = os.path.join(self.target, path)
  667.             st = os.lstat(sourcepath)
  668.             mode = stat.S_IMODE(st.st_mode)
  669.             if stat.S_ISLNK(st.st_mode):
  670.                 if os.path.lexists(targetpath):
  671.                     os.unlink(targetpath)
  672.                 
  673.                 linkto = os.readlink(sourcepath)
  674.                 os.symlink(linkto, targetpath)
  675.             elif stat.S_ISDIR(st.st_mode):
  676.                 if not os.path.isdir(targetpath):
  677.                     os.mkdir(targetpath, mode)
  678.                 
  679.             elif stat.S_ISCHR(st.st_mode):
  680.                 os.mknod(targetpath, stat.S_IFCHR | mode, st.st_rdev)
  681.             elif stat.S_ISBLK(st.st_mode):
  682.                 os.mknod(targetpath, stat.S_IFBLK | mode, st.st_rdev)
  683.             elif stat.S_ISFIFO(st.st_mode):
  684.                 os.mknod(targetpath, stat.S_IFIFO | mode)
  685.             elif stat.S_ISSOCK(st.st_mode):
  686.                 os.mknod(targetpath, stat.S_IFSOCK | mode)
  687.             elif stat.S_ISREG(st.st_mode):
  688.                 if '/%s' % path in self.blacklist:
  689.                     syslog.syslog('Not copying %s' % path)
  690.                     continue
  691.                 
  692.                 if os.path.exists(targetpath):
  693.                     os.unlink(targetpath)
  694.                 
  695.                 self.copy_file(sourcepath, targetpath, md5_check)
  696.             
  697.             copied_size += st.st_size
  698.             os.lchown(targetpath, st.st_uid, st.st_gid)
  699.             if not stat.S_ISLNK(st.st_mode):
  700.                 os.chmod(targetpath, mode)
  701.             
  702.             if stat.S_ISDIR(st.st_mode):
  703.                 directory_times.append((targetpath, st.st_atime, st.st_mtime))
  704.             elif not stat.S_ISLNK(st.st_mode):
  705.                 os.utime(targetpath, (st.st_atime, st.st_mtime))
  706.             
  707.             if int(copied_size * 90 / total_size) != copy_progress:
  708.                 copy_progress = int(copied_size * 90 / total_size)
  709.                 self.db.progress('SET', 10 + copy_progress)
  710.             
  711.             time_now = time.time()
  712.             if time_now - times[-1][0] >= 0.5:
  713.                 times.append((time_now, copied_size))
  714.                 if not long_enough and time_now - times[0][0] >= 10:
  715.                     long_enough = True
  716.                 
  717.                 if long_enough and time_now - time_last_update >= 2:
  718.                     time_last_update = time_now
  719.                     while time_now - times[0][0] > 60 and time_now - times[1][0] >= 60:
  720.                         times.pop(0)
  721.                     speed = (times[-1][1] - times[0][1]) / (times[-1][0] - times[0][0])
  722.                     if speed != 0:
  723.                         time_remaining = int((total_size - copied_size) / speed)
  724.                         if time_remaining < 60:
  725.                             self.db.progress('INFO', 'ubiquity/install/copying_minute')
  726.                         
  727.                     
  728.                 
  729.             time_now - time_last_update >= 2
  730.         
  731.         for dirtime in directory_times:
  732.             (directory, atime, mtime) = dirtime
  733.             
  734.             try:
  735.                 os.utime(directory, (atime, mtime))
  736.             continue
  737.             except OSError:
  738.                 continue
  739.             
  740.  
  741.         
  742.         bootdir = os.path.join(self.target, 'boot')
  743.         kernel = self.find_cd_kernel()
  744.         os.umask(old_umask)
  745.         self.db.progress('SET', 100)
  746.         self.db.progress('STOP')
  747.  
  748.     
  749.     def copy_logs(self):
  750.         '''copy log files into installed system.'''
  751.         target_dir = os.path.join(self.target, 'var/log/installer')
  752.         if not os.path.exists(target_dir):
  753.             os.makedirs(target_dir)
  754.         
  755.         for log_file in ('/var/log/syslog', '/var/log/partman', '/var/log/installer/version', '/var/log/casper.log'):
  756.             target_log_file = os.path.join(target_dir, os.path.basename(log_file))
  757.             if os.path.isfile(log_file):
  758.                 if not misc.execute('cp', '-a', log_file, target_log_file):
  759.                     syslog.syslog(syslog.LOG_ERR, 'Failed to copy installation log file')
  760.                 
  761.                 os.chmod(target_log_file, stat.S_IRUSR | stat.S_IWUSR)
  762.                 continue
  763.         
  764.         
  765.         try:
  766.             status = open(os.path.join(self.target, 'var/lib/dpkg/status'))
  767.             status_gz = gzip.open(os.path.join(target_dir, 'initial-status.gz'), 'w')
  768.             while True:
  769.                 data = status.read(65536)
  770.                 if not data:
  771.                     break
  772.                 
  773.                 status_gz.write(data)
  774.             status_gz.close()
  775.             status.close()
  776.         except IOError:
  777.             pass
  778.  
  779.         
  780.         try:
  781.             if self.db.get('oem-config/enable') == 'true':
  782.                 oem_id = self.db.get('oem-config/id')
  783.                 oem_id_file = open(os.path.join(self.target, 'var/log/installer/oem-id'), 'w')
  784.                 print >>oem_id_file, oem_id
  785.                 oem_id_file.close()
  786.         except (debconf.DebconfError, IOError):
  787.             pass
  788.  
  789.  
  790.     
  791.     def mount_one_image(self, fsfile, mountpoint = None):
  792.         if os.path.splitext(fsfile)[1] == '.cloop':
  793.             blockdev_prefix = 'cloop'
  794.         elif os.path.splitext(fsfile)[1] == '.squashfs':
  795.             blockdev_prefix = 'loop'
  796.         
  797.         if blockdev_prefix == '':
  798.             raise InstallStepError('No source device found for %s' % fsfile)
  799.         
  800.         dev = ''
  801.         sysloops = (filter,)((lambda x: x.startswith(blockdev_prefix)), os.listdir('/sys/block'))
  802.         sysloops.sort()
  803.         for sysloop in sysloops:
  804.             
  805.             try:
  806.                 sysloopf = open(os.path.join('/sys/block', sysloop, 'size'))
  807.                 sysloopsize = sysloopf.readline().strip()
  808.                 sysloopf.close()
  809.                 if sysloopsize == '0':
  810.                     devnull = open('/dev/null')
  811.                     if osextras.find_on_path('udevadm'):
  812.                         udevinfo_cmd = [
  813.                             'udevadm',
  814.                             'info']
  815.                     else:
  816.                         udevinfo_cmd = [
  817.                             'udevinfo']
  818.                     udevinfo_cmd.extend([
  819.                         '-q',
  820.                         'name',
  821.                         '-p',
  822.                         os.path.join('/block', sysloop)])
  823.                     udevinfo = subprocess.Popen(udevinfo_cmd, stdout = subprocess.PIPE, stderr = devnull)
  824.                     devbase = udevinfo.communicate()[0]
  825.                     devnull.close()
  826.                     if udevinfo.returncode != 0:
  827.                         devbase = sysloop
  828.                     
  829.                     dev = '/dev/%s' % devbase
  830.                     break
  831.             continue
  832.             continue
  833.             continue
  834.  
  835.         
  836.         if dev == '':
  837.             raise InstallStepError('No loop device available for %s' % fsfile)
  838.         
  839.         misc.execute('losetup', dev, fsfile)
  840.         if mountpoint is None:
  841.             mountpoint = '/var/lib/ubiquity/%s' % sysloop
  842.         
  843.         if not os.path.isdir(mountpoint):
  844.             os.mkdir(mountpoint)
  845.         
  846.         if not misc.execute('mount', dev, mountpoint):
  847.             misc.execute('losetup', '-d', dev)
  848.             misc.execute('mount', '-o', 'loop', fsfile, mountpoint)
  849.             dev = 'unused'
  850.         
  851.         return (dev, mountpoint)
  852.  
  853.     
  854.     def mount_source(self):
  855.         '''mounting loop system from cloop or squashfs system.'''
  856.         self.devs = []
  857.         self.mountpoints = []
  858.         if not os.path.isdir(self.source):
  859.             syslog.syslog('mkdir %s' % self.source)
  860.             os.mkdir(self.source)
  861.         
  862.         fs_preseed = self.db.get('ubiquity/install/filesystem-images')
  863.         if fs_preseed == '':
  864.             mounts = open('/proc/mounts')
  865.             for line in mounts:
  866.                 (device, fstype) = line.split()[1:3]
  867.                 if fstype == 'squashfs' and os.path.exists(device):
  868.                     misc.execute('mount', '--bind', device, self.source)
  869.                     self.mountpoints.append(self.source)
  870.                     mounts.close()
  871.                     return None
  872.                     continue
  873.             
  874.             mounts.close()
  875.             fsfiles = [
  876.                 '/cdrom/casper/filesystem.cloop',
  877.                 '/cdrom/casper/filesystem.squashfs',
  878.                 '/cdrom/META/META.squashfs',
  879.                 '/live/image/live/filesystem.squashfs']
  880.             for fsfile in fsfiles:
  881.                 if fsfile != '' and os.path.isfile(fsfile):
  882.                     (dev, mountpoint) = self.mount_one_image(fsfile, self.source)
  883.                     self.devs.append(dev)
  884.                     self.mountpoints.append(mountpoint)
  885.                     continue
  886.             
  887.         elif len(fs_preseed.split()) == 1:
  888.             if not os.path.isfile(fs_preseed):
  889.                 raise InstallStepError('Preseeded filesystem image %s not found' % fs_preseed)
  890.                 (dev, mountpoint) = self.mount_one_image(fsfile, self.source)
  891.                 self.devs.append(dev)
  892.                 self.mountpoints.append(mountpoint)
  893.             
  894.         else:
  895.             for fsfile in fs_preseed.split():
  896.                 if not os.path.isfile(fsfile):
  897.                     raise InstallStepError('Preseeded filesystem image %s not found' % fsfile)
  898.                 
  899.                 (dev, mountpoint) = self.mount_one_image(fsfile)
  900.                 self.devs.append(dev)
  901.                 self.mountpoints.append(mountpoint)
  902.             
  903.             if not self.devs:
  904.                 raise AssertionError
  905.             if not self.mountpoints:
  906.                 raise AssertionError
  907.             misc.execute('mount', '-t', 'unionfs', '-o', 'dirs=' + ':'.join(map((lambda x: '%s=ro' % x), self.mountpoints)), 'unionfs', self.source)
  908.             self.mountpoints.append(self.source)
  909.  
  910.     
  911.     def umount_source(self):
  912.         '''umounting loop system from cloop or squashfs system.'''
  913.         devs = self.devs
  914.         devs.reverse()
  915.         mountpoints = self.mountpoints
  916.         mountpoints.reverse()
  917.         for mountpoint in mountpoints:
  918.             if not misc.execute('umount', mountpoint):
  919.                 raise InstallStepError('Failed to unmount %s' % mountpoint)
  920.                 continue
  921.         
  922.         for dev in devs:
  923.             if dev != '' and dev != 'unused' and not misc.execute('losetup', '-d', dev):
  924.                 raise InstallStepError('Failed to detach loopback device %s' % dev)
  925.                 continue
  926.         
  927.  
  928.     
  929.     def chroot_setup(self, x11 = False):
  930.         '''Set up /target for safe package management operations.'''
  931.         policy_rc_d = os.path.join(self.target, 'usr/sbin/policy-rc.d')
  932.         f = open(policy_rc_d, 'w')
  933.         print >>f, '#!/bin/sh\nexit 101'
  934.         f.close()
  935.         os.chmod(policy_rc_d, 493)
  936.         start_stop_daemon = os.path.join(self.target, 'sbin/start-stop-daemon')
  937.         if os.path.exists(start_stop_daemon):
  938.             os.rename(start_stop_daemon, '%s.REAL' % start_stop_daemon)
  939.         
  940.         f = open(start_stop_daemon, 'w')
  941.         print >>f, "#!/bin/sh\necho 1>&2\necho 'Warning: Fake start-stop-daemon called, doing nothing.' 1>&2\nexit 0"
  942.         f.close()
  943.         os.chmod(start_stop_daemon, 493)
  944.         if not os.path.exists(os.path.join(self.target, 'proc/cmdline')):
  945.             self.chrex('mount', '-t', 'proc', 'proc', '/proc')
  946.         
  947.         if not os.path.exists(os.path.join(self.target, 'sys/devices')):
  948.             self.chrex('mount', '-t', 'sysfs', 'sysfs', '/sys')
  949.         
  950.         if x11 and 'DISPLAY' in os.environ:
  951.             if 'SUDO_USER' in os.environ:
  952.                 xauthority = os.path.expanduser('~%s/.Xauthority' % os.environ['SUDO_USER'])
  953.             else:
  954.                 xauthority = os.path.expanduser('~/.Xauthority')
  955.             if os.path.exists(xauthority):
  956.                 shutil.copy(xauthority, os.path.join(self.target, 'root/.Xauthority'))
  957.             
  958.             if not os.path.isdir(os.path.join(self.target, 'tmp/.X11-unix')):
  959.                 os.mkdir(os.path.join(self.target, 'tmp/.X11-unix'))
  960.             
  961.             misc.execute('mount', '--bind', '/tmp/.X11-unix', os.path.join(self.target, 'tmp/.X11-unix'))
  962.         
  963.  
  964.     
  965.     def chroot_cleanup(self, x11 = False):
  966.         '''Undo the work done by chroot_setup.'''
  967.         if x11 and 'DISPLAY' in os.environ:
  968.             misc.execute('umount', os.path.join(self.target, 'tmp/.X11-unix'))
  969.             
  970.             try:
  971.                 os.rmdir(os.path.join(self.target, 'tmp/.X11-unix'))
  972.             except OSError:
  973.                 pass
  974.  
  975.             
  976.             try:
  977.                 os.unlink(os.path.join(self.target, 'root/.Xauthority'))
  978.             except OSError:
  979.                 pass
  980.             except:
  981.                 None<EXCEPTION MATCH>OSError
  982.             
  983.  
  984.         None<EXCEPTION MATCH>OSError
  985.         self.chrex('umount', '/sys')
  986.         self.chrex('umount', '/proc')
  987.         start_stop_daemon = os.path.join(self.target, 'sbin/start-stop-daemon')
  988.         os.rename('%s.REAL' % start_stop_daemon, start_stop_daemon)
  989.         policy_rc_d = os.path.join(self.target, 'usr/sbin/policy-rc.d')
  990.         os.unlink(policy_rc_d)
  991.  
  992.     
  993.     def run_target_config_hooks(self):
  994.         '''Run hook scripts from /usr/lib/ubiquity/target-config. This allows
  995.         casper to hook into us and repeat bits of its configuration in the
  996.         target system.'''
  997.         hookdir = '/usr/lib/ubiquity/target-config'
  998.         if os.path.isdir(hookdir):
  999.             hooks = filter((lambda entry: '.' not in entry), os.listdir(hookdir))
  1000.             self.db.progress('START', 0, len(hooks), 'ubiquity/install/title')
  1001.             self.db.progress('INFO', 'ubiquity/install/target_hooks')
  1002.             for hookentry in hooks:
  1003.                 hook = os.path.join(hookdir, hookentry)
  1004.                 if not os.access(hook, os.X_OK):
  1005.                     self.db.progress('STEP', 1)
  1006.                     continue
  1007.                 
  1008.                 subprocess.call([
  1009.                     'log-output',
  1010.                     '-t',
  1011.                     'ubiquity',
  1012.                     '--pass-stdout',
  1013.                     hook])
  1014.                 self.db.progress('STEP', 1)
  1015.             
  1016.             self.db.progress('STOP')
  1017.         
  1018.  
  1019.     
  1020.     def configure_locales(self):
  1021.         '''Apply locale settings to installed system.'''
  1022.         dbfilter = language_apply.LanguageApply(None)
  1023.         ret = dbfilter.run_command(auto_process = True)
  1024.         if ret != 0:
  1025.             raise InstallStepError('LanguageApply failed with code %d' % ret)
  1026.         
  1027.         self.chrex('fontconfig-voodoo', '--auto', '--force', '--quiet')
  1028.  
  1029.     
  1030.     def configure_apt(self):
  1031.         '''Configure /etc/apt/sources.list.'''
  1032.         apt_conf_tc = open(os.path.join(self.target, 'etc/apt/apt.conf.d/00trustcdrom'), 'w')
  1033.         print >>apt_conf_tc, 'APT::Authentication::TrustCDROM "true";'
  1034.         apt_conf_tc.close()
  1035.         apt_conf_itc = open(os.path.join(self.target, 'etc/apt/apt.conf.d/00IgnoreTimeConflict'), 'w')
  1036.         print >>apt_conf_itc, 'Acquire::gpgv::Options { "--ignore-time-conflict"; };'
  1037.         apt_conf_itc.close()
  1038.         
  1039.         try:
  1040.             if self.db.get('debian-installer/allow_unauthenticated') == 'true':
  1041.                 apt_conf_au = open(os.path.join(self.target, 'etc/apt/apt.conf.d/00AllowUnauthenticated'), 'w')
  1042.                 print >>apt_conf_au, 'APT::Get::AllowUnauthenticated "true";'
  1043.                 print >>apt_conf_au, 'Aptitude::CmdLine::Ignore-Trust-Violations "true";'
  1044.                 apt_conf_au.close()
  1045.         except debconf.DebconfError:
  1046.             pass
  1047.  
  1048.         target_cdrom = os.path.join(self.target, 'cdrom')
  1049.         misc.execute('umount', target_cdrom)
  1050.         if not os.path.exists(target_cdrom):
  1051.             os.mkdir(target_cdrom)
  1052.         
  1053.         misc.execute('mount', '--bind', '/cdrom', target_cdrom)
  1054.         apt_conf_nmc = open(os.path.join(self.target, 'etc/apt/apt.conf.d/00NoMountCDROM'), 'w')
  1055.         print >>apt_conf_nmc, textwrap.dedent('            APT::CDROM::NoMount "true";\n            Acquire::cdrom {\n              mount "/cdrom";\n              "/cdrom/" {\n                Mount  "true";\n                UMount "true";\n              };\n            }')
  1056.         apt_conf_nmc.close()
  1057.         dbfilter = apt_setup.AptSetup(None, self.db)
  1058.         ret = dbfilter.run_command(auto_process = True)
  1059.         if ret != 0:
  1060.             raise InstallStepError('AptSetup failed with code %d' % ret)
  1061.         
  1062.  
  1063.     
  1064.     def get_cache_pkg(self, cache, pkg):
  1065.         
  1066.         try:
  1067.             return cache[pkg]
  1068.         except KeyError:
  1069.             return None
  1070.  
  1071.  
  1072.     
  1073.     def record_installed(self, pkgs):
  1074.         """Record which packages we've explicitly installed so that we don't
  1075.         try to remove them later."""
  1076.         record_file = '/var/lib/ubiquity/apt-installed'
  1077.         if not os.path.exists(os.path.dirname(record_file)):
  1078.             os.makedirs(os.path.dirname(record_file))
  1079.         
  1080.         record = open(record_file, 'a')
  1081.         for pkg in pkgs:
  1082.             print >>record, pkg
  1083.         
  1084.         record.close()
  1085.  
  1086.     
  1087.     def query_recorded_installed(self):
  1088.         apt_installed = set()
  1089.         if os.path.exists('/var/lib/ubiquity/apt-installed'):
  1090.             record_file = open('/var/lib/ubiquity/apt-installed')
  1091.             for line in record_file:
  1092.                 apt_installed.add(line.strip())
  1093.             
  1094.             record_file.close()
  1095.         
  1096.         return apt_installed
  1097.  
  1098.     
  1099.     def mark_install(self, cache, pkg):
  1100.         cachedpkg = self.get_cache_pkg(cache, pkg)
  1101.         if cachedpkg is not None and not (cachedpkg.isInstalled):
  1102.             apt_error = False
  1103.             
  1104.             try:
  1105.                 cachedpkg.markInstall()
  1106.             except SystemError:
  1107.                 apt_error = True
  1108.  
  1109.             if cache._depcache.BrokenCount > 0 or apt_error:
  1110.                 brokenpkgs = self.broken_packages(cache)
  1111.                 while brokenpkgs:
  1112.                     for brokenpkg in brokenpkgs:
  1113.                         self.get_cache_pkg(cache, brokenpkg).markKeep()
  1114.                     
  1115.                     new_brokenpkgs = self.broken_packages(cache)
  1116.                     if brokenpkgs == new_brokenpkgs:
  1117.                         break
  1118.                     
  1119.                     brokenpkgs = new_brokenpkgs
  1120.                 if not cache._depcache.BrokenCount == 0:
  1121.                     raise AssertionError
  1122.             
  1123.         
  1124.  
  1125.     
  1126.     def select_language_packs(self):
  1127.         langpacks = []
  1128.         
  1129.         try:
  1130.             langpack_db = self.db.get('pkgsel/language-packs')
  1131.             langpacks = langpack_db.replace(',', '').split()
  1132.         except debconf.DebconfError:
  1133.             pass
  1134.  
  1135.         if not langpacks:
  1136.             
  1137.             try:
  1138.                 langpack_db = self.db.get('localechooser/supported-locales')
  1139.                 langpack_set = set()
  1140.                 for locale in langpack_db.replace(',', '').split():
  1141.                     langpack_set.add(locale.split('_')[0])
  1142.                 
  1143.                 langpacks = sorted(langpack_set)
  1144.             except debconf.DebconfError:
  1145.                 pass
  1146.             except:
  1147.                 None<EXCEPTION MATCH>debconf.DebconfError
  1148.             
  1149.  
  1150.         None<EXCEPTION MATCH>debconf.DebconfError
  1151.         if not langpacks:
  1152.             langpack_db = self.db.get('debian-installer/locale')
  1153.             langpacks = [
  1154.                 langpack_db.split('_')[0]]
  1155.         
  1156.         syslog.syslog('keeping language packs for: %s' % ' '.join(langpacks))
  1157.         
  1158.         try:
  1159.             lppatterns = self.db.get('pkgsel/language-pack-patterns').split()
  1160.         except debconf.DebconfError:
  1161.             return None
  1162.  
  1163.         to_install = []
  1164.         for lp in langpacks:
  1165.             to_install.append('language-pack-%s' % lp)
  1166.             for pattern in lppatterns:
  1167.                 to_install.append(pattern.replace('$LL', lp))
  1168.             
  1169.             to_install.append('language-support-%s' % lp)
  1170.         
  1171.         self.record_installed(to_install)
  1172.         self.langpacks = to_install
  1173.  
  1174.     
  1175.     def install_language_packs(self):
  1176.         self.do_install(self.langpacks)
  1177.         cache = Cache()
  1178.         incomplete = False
  1179.         for pkg in self.langpacks:
  1180.             cachedpkg = self.get_cache_pkg(cache, pkg)
  1181.             if cachedpkg is None or not (cachedpkg.isInstalled):
  1182.                 incomplete = True
  1183.                 break
  1184.                 continue
  1185.         
  1186.         if incomplete:
  1187.             language_support_dir = os.path.join(self.target, 'usr/share/language-support')
  1188.             update_notifier_dir = os.path.join(self.target, 'var/lib/update-notifier/user.d')
  1189.             for note in ('incomplete-language-support-gnome.note', 'incomplete-language-support-qt.note'):
  1190.                 notepath = os.path.join(language_support_dir, note)
  1191.                 if os.path.exists(notepath):
  1192.                     if not os.path.exists(update_notifier_dir):
  1193.                         os.makedirs(update_notifier_dir)
  1194.                     
  1195.                     shutil.copy(notepath, os.path.join(update_notifier_dir, note))
  1196.                     break
  1197.                     continue
  1198.             
  1199.         
  1200.  
  1201.     
  1202.     def configure_timezone(self):
  1203.         '''Set timezone on installed system.'''
  1204.         dbfilter = timezone_apply.TimezoneApply(None)
  1205.         ret = dbfilter.run_command(auto_process = True)
  1206.         if ret != 0:
  1207.             raise InstallStepError('TimezoneApply failed with code %d' % ret)
  1208.         
  1209.         dbfilter = clock_setup.ClockSetup(None)
  1210.         ret = dbfilter.run_command(auto_process = True)
  1211.         if ret != 0:
  1212.             raise InstallStepError('ClockSetup failed with code %d' % ret)
  1213.         
  1214.  
  1215.     
  1216.     def configure_keyboard(self):
  1217.         '''Set keyboard in installed system.'''
  1218.         dbfilter = console_setup_apply.ConsoleSetupApply(None)
  1219.         ret = dbfilter.run_command(auto_process = True)
  1220.         if ret != 0:
  1221.             raise InstallStepError('ConsoleSetupApply failed with code %d' % ret)
  1222.         
  1223.  
  1224.     
  1225.     def configure_user(self):
  1226.         '''create the user selected along the installation process
  1227.         into the installed system. Default user from live system is
  1228.         deleted and skel for this new user is copied to $HOME.'''
  1229.         dbfilter = usersetup_apply.UserSetupApply(None)
  1230.         ret = dbfilter.run_command(auto_process = True)
  1231.         if ret != 0:
  1232.             raise InstallStepError('UserSetupApply failed with code %d' % ret)
  1233.         
  1234.  
  1235.     
  1236.     def configure_ma(self):
  1237.         '''import documents, settings, and users from previous operating
  1238.         systems.'''
  1239.         if 'UBIQUITY_MIGRATION_ASSISTANT' in os.environ:
  1240.             dbfilter = migrationassistant_apply.MigrationAssistantApply(None)
  1241.             ret = dbfilter.run_command(auto_process = True)
  1242.             if ret != 0:
  1243.                 raise InstallStepError('MigrationAssistantApply failed with code %d' % ret)
  1244.             
  1245.         
  1246.  
  1247.     
  1248.     def get_resume_partition(self):
  1249.         biggest_size = 0
  1250.         biggest_partition = None
  1251.         swaps = open('/proc/swaps')
  1252.         for line in swaps:
  1253.             words = line.split()
  1254.             if words[1] != 'partition':
  1255.                 continue
  1256.             
  1257.             if words[0].startswith('/dev/ramzswap'):
  1258.                 continue
  1259.             
  1260.             size = int(words[2])
  1261.             if size > biggest_size:
  1262.                 biggest_size = size
  1263.                 biggest_partition = words[0]
  1264.                 continue
  1265.         
  1266.         swaps.close()
  1267.         return biggest_partition
  1268.  
  1269.     
  1270.     def configure_hardware(self):
  1271.         '''reconfiguring several packages which depends on the
  1272.         hardware system in which has been installed on and need some
  1273.         automatic configurations to get work.'''
  1274.         self.chroot_setup()
  1275.         
  1276.         try:
  1277.             dbfilter = hw_detect.HwDetect(None, self.db)
  1278.             ret = dbfilter.run_command(auto_process = True)
  1279.             if ret != 0:
  1280.                 raise InstallStepError('HwDetect failed with code %d' % ret)
  1281.         finally:
  1282.             self.chroot_cleanup()
  1283.  
  1284.         self.db.progress('INFO', 'ubiquity/install/hardware')
  1285.         misc.execute('/usr/lib/ubiquity/debian-installer-utils/register-module.post-base-installer')
  1286.         resume = self.get_resume_partition()
  1287.         if resume is not None:
  1288.             resume_uuid = None
  1289.             
  1290.             try:
  1291.                 resume_uuid = subprocess.Popen([
  1292.                     'vol_id',
  1293.                     '-u',
  1294.                     resume], stdout = subprocess.PIPE).communicate()[0].rstrip('\n')
  1295.             except OSError:
  1296.                 pass
  1297.  
  1298.             if resume_uuid:
  1299.                 resume = 'UUID=%s' % resume_uuid
  1300.             
  1301.             if os.path.exists(os.path.join(self.target, 'etc/initramfs-tools/conf.d')):
  1302.                 configdir = os.path.join(self.target, 'etc/initramfs-tools/conf.d')
  1303.             elif os.path.exists(os.path.join(self.target, 'etc/mkinitramfs/conf.d')):
  1304.                 configdir = os.path.join(self.target, 'etc/mkinitramfs/conf.d')
  1305.             else:
  1306.                 configdir = None
  1307.             if configdir is not None:
  1308.                 configfile = open(os.path.join(configdir, 'resume'), 'w')
  1309.                 print >>configfile, 'RESUME=%s' % resume
  1310.                 configfile.close()
  1311.             
  1312.         
  1313.         
  1314.         try:
  1315.             os.unlink('/target/etc/usplash.conf')
  1316.         except OSError:
  1317.             pass
  1318.  
  1319.         
  1320.         try:
  1321.             modes = self.db.get('xserver-xorg/config/display/modes')
  1322.             self.set_debconf('xserver-xorg/config/display/modes', modes)
  1323.         except debconf.DebconfError:
  1324.             pass
  1325.  
  1326.         
  1327.         try:
  1328.             os.unlink('/target/etc/popularity-contest.conf')
  1329.         except OSError:
  1330.             pass
  1331.  
  1332.         
  1333.         try:
  1334.             participate = self.db.get('popularity-contest/participate')
  1335.             self.set_debconf('popularity-contest/participate', participate)
  1336.         except debconf.DebconfError:
  1337.             pass
  1338.  
  1339.         
  1340.         try:
  1341.             os.unlink('/target/etc/papersize')
  1342.         except OSError:
  1343.             pass
  1344.  
  1345.         subprocess.call([
  1346.             'log-output',
  1347.             '-t',
  1348.             'ubiquity',
  1349.             'chroot',
  1350.             self.target,
  1351.             'ucf',
  1352.             '--purge',
  1353.             '/etc/papersize'], preexec_fn = debconf_disconnect, close_fds = True)
  1354.         
  1355.         try:
  1356.             self.set_debconf('libpaper/defaultpaper', '')
  1357.         except debconf.DebconfError:
  1358.             pass
  1359.  
  1360.         
  1361.         try:
  1362.             os.unlink('/target/etc/ssl/certs/ssl-cert-snakeoil.pem')
  1363.         except OSError:
  1364.             pass
  1365.  
  1366.         
  1367.         try:
  1368.             os.unlink('/target/etc/ssl/private/ssl-cert-snakeoil.key')
  1369.         except OSError:
  1370.             pass
  1371.  
  1372.         self.chroot_setup(x11 = True)
  1373.         self.chrex('dpkg-divert', '--package', 'ubiquity', '--rename', '--quiet', '--add', '/usr/sbin/update-initramfs')
  1374.         
  1375.         try:
  1376.             os.symlink('/bin/true', '/target/usr/sbin/update-initramfs')
  1377.         except OSError:
  1378.             pass
  1379.  
  1380.         packages = [
  1381.             'linux-image-' + self.kernel_version,
  1382.             'linux-restricted-modules-' + self.kernel_version,
  1383.             'usplash',
  1384.             'splashy',
  1385.             'popularity-contest',
  1386.             'libpaper1',
  1387.             'ssl-cert']
  1388.         
  1389.         try:
  1390.             for package in packages:
  1391.                 self.reconfigure(package)
  1392.         finally:
  1393.             
  1394.             try:
  1395.                 os.unlink('/target/usr/sbin/update-initramfs')
  1396.             except OSError:
  1397.                 pass
  1398.  
  1399.             self.chrex('dpkg-divert', '--package', 'ubiquity', '--rename', '--quiet', '--remove', '/usr/sbin/update-initramfs')
  1400.             self.chrex('update-initramfs', '-c', '-k', os.uname()[2])
  1401.             self.chroot_cleanup(x11 = True)
  1402.  
  1403.         bootdir = os.path.join(self.target, 'boot')
  1404.         if self.db.get('base-installer/kernel/linux/link_in_boot') == 'true':
  1405.             linkdir = bootdir
  1406.             linkprefix = ''
  1407.         else:
  1408.             linkdir = self.target
  1409.             linkprefix = 'boot'
  1410.         re_symlink = re.compile('vmlinu[xz]|initrd.img$')
  1411.         for entry in os.listdir(linkdir):
  1412.             if re_symlink.match(entry) is not None:
  1413.                 filename = os.path.join(linkdir, entry)
  1414.                 if os.path.islink(filename):
  1415.                     os.unlink(filename)
  1416.                 
  1417.             os.path.islink(filename)
  1418.         
  1419.         if linkdir != self.target:
  1420.             for entry in os.listdir(self.target):
  1421.                 if re_symlink.match(entry) is not None:
  1422.                     filename = os.path.join(self.target, entry)
  1423.                     if os.path.islink(filename):
  1424.                         os.unlink(filename)
  1425.                     
  1426.                 os.path.islink(filename)
  1427.             
  1428.         
  1429.         re_image = re.compile('(vmlinu[xz]|initrd.img)-')
  1430.         for entry in os.listdir(bootdir):
  1431.             match = re_image.match(entry)
  1432.             if match is not None:
  1433.                 imagetype = match.group(1)
  1434.                 linksrc = os.path.join(linkprefix, entry)
  1435.                 linkdst = os.path.join(linkdir, imagetype)
  1436.                 if os.path.exists(linkdst):
  1437.                     if entry.endswith('-' + self.kernel_version):
  1438.                         os.unlink(linkdst)
  1439.                     
  1440.                 
  1441.                 os.symlink(linksrc, linkdst)
  1442.                 continue
  1443.         
  1444.  
  1445.     
  1446.     def get_all_interfaces(self):
  1447.         '''Get all non-local network interfaces.'''
  1448.         ifs = []
  1449.         ifs_file = open('/proc/net/dev')
  1450.         ifs_file.readline()
  1451.         ifs_file.readline()
  1452.         for line in ifs_file:
  1453.             name = re.match('(.*?(?::\\d+)?):', line.strip()).group(1)
  1454.             if name == 'lo':
  1455.                 continue
  1456.             
  1457.             ifs.append(name)
  1458.         
  1459.         ifs_file.close()
  1460.         return ifs
  1461.  
  1462.     
  1463.     def configure_network(self):
  1464.         """Automatically configure the network.
  1465.  
  1466.         At present, the only thing the user gets to tweak in the UI is the
  1467.         hostname. Some other things will be copied from the live filesystem,
  1468.         so changes made there will be reflected in the installed system.
  1469.  
  1470.         Unfortunately, at present we have to duplicate a fair bit of netcfg
  1471.         here, because it's hard to drive netcfg in a way that won't try to
  1472.         bring interfaces up and down."""
  1473.         for path in ('/etc/network/interfaces', '/etc/resolv.conf'):
  1474.             if os.path.exists(path):
  1475.                 shutil.copy2(path, os.path.join(self.target, path[1:]))
  1476.                 continue
  1477.         
  1478.         
  1479.         try:
  1480.             hostname = self.db.get('netcfg/get_hostname')
  1481.         except debconf.DebconfError:
  1482.             hostname = ''
  1483.  
  1484.         
  1485.         try:
  1486.             domain = self.db.get('netcfg/get_domain')
  1487.         except debconf.DebconfError:
  1488.             domain = ''
  1489.  
  1490.         if hostname == '':
  1491.             hostname = 'ubuntu'
  1492.         
  1493.         fp = open(os.path.join(self.target, 'etc/hostname'), 'w')
  1494.         print >>fp, hostname
  1495.         fp.close()
  1496.         hosts = open(os.path.join(self.target, 'etc/hosts'), 'w')
  1497.         print >>hosts, '127.0.0.1\tlocalhost'
  1498.         if domain:
  1499.             print >>hosts, '127.0.1.1\t%s.%s\t%s' % (hostname, domain, hostname)
  1500.         else:
  1501.             print >>hosts, '127.0.1.1\t%s' % hostname
  1502.         print >>hosts, textwrap.dedent('\n            # The following lines are desirable for IPv6 capable hosts\n            ::1     ip6-localhost ip6-loopback\n            fe00::0 ip6-localnet\n            ff00::0 ip6-mcastprefix\n            ff02::1 ip6-allnodes\n            ff02::2 ip6-allrouters\n            ff02::3 ip6-allhosts')
  1503.         hosts.close()
  1504.         persistent_net = '/etc/udev/rules.d/70-persistent-net.rules'
  1505.  
  1506.     
  1507.     def configure_bootloader(self):
  1508.         '''configuring and installing boot loader into installed
  1509.         hardware system.'''
  1510.         install_bootloader = self.db.get('ubiquity/install_bootloader')
  1511.         if install_bootloader == 'true':
  1512.             misc.execute('mount', '--bind', '/proc', self.target + '/proc')
  1513.             misc.execute('mount', '--bind', '/dev', self.target + '/dev')
  1514.             archdetect = subprocess.Popen([
  1515.                 'archdetect'], stdout = subprocess.PIPE)
  1516.             subarch = archdetect.communicate()[0].strip()
  1517.             
  1518.             try:
  1519.                 if subarch.startswith('amd64/') and subarch.startswith('i386/') or subarch.startswith('lpia/'):
  1520.                     grubinstaller = grubinstaller
  1521.                     import ubiquity.components
  1522.                     dbfilter = grubinstaller.GrubInstaller(None)
  1523.                     ret = dbfilter.run_command(auto_process = True)
  1524.                     if ret != 0:
  1525.                         raise InstallStepError('GrubInstaller failed with code %d' % ret)
  1526.                     
  1527.                 elif subarch == 'powerpc/ps3':
  1528.                     kbootinstaller = kbootinstaller
  1529.                     import ubiquity.components
  1530.                     dbfilter = kbootinstaller.KbootInstaller(None)
  1531.                     ret = dbfilter.run_command(auto_process = True)
  1532.                     if ret != 0:
  1533.                         raise InstallStepError('KbootInstaller failed with code %d' % ret)
  1534.                     
  1535.                 elif subarch.startswith('powerpc/'):
  1536.                     yabootinstaller = yabootinstaller
  1537.                     import ubiquity.components
  1538.                     dbfilter = yabootinstaller.YabootInstaller(None)
  1539.                     ret = dbfilter.run_command(auto_process = True)
  1540.                     if ret != 0:
  1541.                         raise InstallStepError('YabootInstaller failed with code %d' % ret)
  1542.                     
  1543.                 else:
  1544.                     raise InstallStepError('No bootloader installer found')
  1545.             except ImportError:
  1546.                 raise InstallStepError('No bootloader installer found')
  1547.  
  1548.             misc.execute('umount', '-f', self.target + '/proc')
  1549.             misc.execute('umount', '-f', self.target + '/dev')
  1550.         
  1551.  
  1552.     
  1553.     def broken_packages(self, cache):
  1554.         brokenpkgs = set()
  1555.         for pkg in cache.keys():
  1556.             
  1557.             try:
  1558.                 if cache._depcache.IsInstBroken(cache._cache[pkg]):
  1559.                     brokenpkgs.add(pkg)
  1560.             continue
  1561.             except KeyError:
  1562.                 continue
  1563.                 continue
  1564.             
  1565.  
  1566.         
  1567.         return brokenpkgs
  1568.  
  1569.     
  1570.     def do_install(self, to_install):
  1571.         if self.langpacks:
  1572.             self.db.progress('START', 0, 10, 'ubiquity/langpacks/title')
  1573.         else:
  1574.             self.db.progress('START', 0, 10, 'ubiquity/install/title')
  1575.         self.db.progress('INFO', 'ubiquity/install/find_installables')
  1576.         self.db.progress('REGION', 0, 1)
  1577.         fetchprogress = DebconfFetchProgress(self.db, 'ubiquity/install/title', 'ubiquity/install/apt_indices_starting', 'ubiquity/install/apt_indices')
  1578.         cache = Cache()
  1579.         if cache._depcache.BrokenCount > 0:
  1580.             syslog.syslog('not installing additional packages, since there are broken packages: %s' % ', '.join(self.broken_packages(cache)))
  1581.             self.db.progress('STOP')
  1582.             return None
  1583.         
  1584.         for pkg in to_install:
  1585.             self.mark_install(cache, pkg)
  1586.         
  1587.         self.db.progress('SET', 1)
  1588.         self.db.progress('REGION', 1, 10)
  1589.         if self.langpacks:
  1590.             fetchprogress = DebconfFetchProgress(self.db, 'ubiquity/langpacks/title', None, 'ubiquity/langpacks/packages')
  1591.             installprogress = DebconfInstallProgress(self.db, 'ubiquity/langpacks/title', 'ubiquity/install/apt_info')
  1592.         else:
  1593.             fetchprogress = DebconfFetchProgress(self.db, 'ubiquity/install/title', None, 'ubiquity/install/fetch_remove')
  1594.             installprogress = DebconfInstallProgress(self.db, 'ubiquity/install/title', 'ubiquity/install/apt_info', 'ubiquity/install/apt_error_install')
  1595.         self.chroot_setup()
  1596.         commit_error = None
  1597.         
  1598.         try:
  1599.             if not cache.commit(fetchprogress, installprogress):
  1600.                 fetchprogress.stop()
  1601.                 installprogress.finishUpdate()
  1602.                 self.db.progress('STOP')
  1603.                 return None
  1604.         except IOError:
  1605.             e = None
  1606.             for line in str(e).split('\n'):
  1607.                 syslog.syslog(syslog.LOG_ERR, line)
  1608.             
  1609.             fetchprogress.stop()
  1610.             installprogress.finishUpdate()
  1611.             self.db.progress('STOP')
  1612.             return None
  1613.         except SystemError:
  1614.             e = None
  1615.             for line in str(e).split('\n'):
  1616.                 syslog.syslog(syslog.LOG_ERR, line)
  1617.             
  1618.             commit_error = str(e)
  1619.         finally:
  1620.             self.chroot_cleanup()
  1621.  
  1622.         self.db.progress('SET', 10)
  1623.         cache.open(None)
  1624.         if commit_error or cache._depcache.BrokenCount > 0:
  1625.             if commit_error is None:
  1626.                 commit_error = ''
  1627.             
  1628.             brokenpkgs = self.broken_packages(cache)
  1629.             syslog.syslog('broken packages after installation: %s' % ', '.join(brokenpkgs))
  1630.             self.db.subst('ubiquity/install/broken_install', 'ERROR', commit_error)
  1631.             self.db.subst('ubiquity/install/broken_install', 'PACKAGES', ', '.join(brokenpkgs))
  1632.             self.db.input('critical', 'ubiquity/install/broken_install')
  1633.             self.db.go()
  1634.         
  1635.         self.db.progress('STOP')
  1636.  
  1637.     
  1638.     def get_remove_list(self, cache, to_remove, recursive = False):
  1639.         to_remove = set(to_remove)
  1640.         all_removed = set()
  1641.         while True:
  1642.             removed = set()
  1643.             for pkg in to_remove:
  1644.                 cachedpkg = self.get_cache_pkg(cache, pkg)
  1645.                 if cachedpkg is not None and cachedpkg.isInstalled:
  1646.                     apt_error = False
  1647.                     
  1648.                     try:
  1649.                         cachedpkg.markDelete(autoFix = False, purge = True)
  1650.                     except SystemError:
  1651.                         apt_error = True
  1652.  
  1653.                     if apt_error:
  1654.                         cachedpkg.markKeep()
  1655.                     elif cache._depcache.BrokenCount > 0:
  1656.                         brokenpkgs = self.broken_packages(cache)
  1657.                         broken_removed = set()
  1658.                         while brokenpkgs:
  1659.                             if recursive or brokenpkgs <= to_remove:
  1660.                                 broken_removed_inner = set()
  1661.                                 for pkg2 in brokenpkgs:
  1662.                                     cachedpkg2 = self.get_cache_pkg(cache, pkg2)
  1663.                                     if cachedpkg2 is not None:
  1664.                                         broken_removed_inner.add(pkg2)
  1665.                                         
  1666.                                         try:
  1667.                                             cachedpkg2.markDelete(autoFix = False, purge = True)
  1668.                                         except SystemError:
  1669.                                             apt_error = True
  1670.                                             break
  1671.                                         except:
  1672.                                             None<EXCEPTION MATCH>SystemError
  1673.                                         
  1674.  
  1675.                                     None<EXCEPTION MATCH>SystemError
  1676.                                 
  1677.                                 broken_removed |= broken_removed_inner
  1678.                                 if apt_error or not broken_removed_inner:
  1679.                                     break
  1680.                                 
  1681.                                 brokenpkgs = self.broken_packages(cache)
  1682.                                 continue
  1683.                     if apt_error or cache._depcache.BrokenCount > 0:
  1684.                         for pkg2 in broken_removed:
  1685.                             self.get_cache_pkg(cache, pkg2).markKeep()
  1686.                         
  1687.                         cachedpkg.markKeep()
  1688.                     else:
  1689.                         removed.add(pkg)
  1690.                         removed |= broken_removed
  1691.                 else:
  1692.                     removed.add(pkg)
  1693.                 if not cache._depcache.BrokenCount == 0:
  1694.                     raise AssertionError
  1695.                 continue
  1696.             
  1697.             if not removed:
  1698.                 break
  1699.             
  1700.             to_remove -= removed
  1701.             all_removed |= removed
  1702.         return all_removed
  1703.  
  1704.     
  1705.     def do_remove(self, to_remove, recursive = False):
  1706.         self.db.progress('START', 0, 5, 'ubiquity/install/title')
  1707.         self.db.progress('INFO', 'ubiquity/install/find_removables')
  1708.         fetchprogress = DebconfFetchProgress(self.db, 'ubiquity/install/title', 'ubiquity/install/apt_indices_starting', 'ubiquity/install/apt_indices')
  1709.         cache = Cache()
  1710.         if cache._depcache.BrokenCount > 0:
  1711.             syslog.syslog('not processing removals, since there are broken packages: %s' % ', '.join(self.broken_packages(cache)))
  1712.             self.db.progress('STOP')
  1713.             return None
  1714.         
  1715.         self.get_remove_list(cache, to_remove, recursive)
  1716.         self.db.progress('SET', 1)
  1717.         self.db.progress('REGION', 1, 5)
  1718.         fetchprogress = DebconfFetchProgress(self.db, 'ubiquity/install/title', None, 'ubiquity/install/fetch_remove')
  1719.         installprogress = DebconfInstallProgress(self.db, 'ubiquity/install/title', 'ubiquity/install/apt_info', 'ubiquity/install/apt_error_remove')
  1720.         self.chroot_setup()
  1721.         commit_error = None
  1722.         
  1723.         try:
  1724.             if not cache.commit(fetchprogress, installprogress):
  1725.                 fetchprogress.stop()
  1726.                 installprogress.finishUpdate()
  1727.                 self.db.progress('STOP')
  1728.                 return None
  1729.         except SystemError:
  1730.             e = None
  1731.             for line in str(e).split('\n'):
  1732.                 syslog.syslog(syslog.LOG_ERR, line)
  1733.             
  1734.             commit_error = str(e)
  1735.         finally:
  1736.             self.chroot_cleanup()
  1737.  
  1738.         self.db.progress('SET', 5)
  1739.         cache.open(None)
  1740.         if commit_error or cache._depcache.BrokenCount > 0:
  1741.             if commit_error is None:
  1742.                 commit_error = ''
  1743.             
  1744.             brokenpkgs = self.broken_packages(cache)
  1745.             syslog.syslog('broken packages after removal: %s' % ', '.join(brokenpkgs))
  1746.             self.db.subst('ubiquity/install/broken_remove', 'ERROR', commit_error)
  1747.             self.db.subst('ubiquity/install/broken_remove', 'PACKAGES', ', '.join(brokenpkgs))
  1748.             self.db.input('critical', 'ubiquity/install/broken_remove')
  1749.             self.db.go()
  1750.         
  1751.         self.db.progress('STOP')
  1752.  
  1753.     
  1754.     def remove_unusable_kernels(self):
  1755.         '''Remove unusable kernels; keeping them may cause us to be unable
  1756.         to boot.'''
  1757.         self.db.progress('START', 0, 5, 'ubiquity/install/title')
  1758.         self.db.progress('INFO', 'ubiquity/install/find_removables')
  1759.         dbfilter = check_kernels.CheckKernels(None)
  1760.         dbfilter.run_command(auto_process = True)
  1761.         remove_kernels = set()
  1762.         if os.path.exists('/var/lib/ubiquity/remove-kernels'):
  1763.             remove_kernels_file = open('/var/lib/ubiquity/remove-kernels')
  1764.             for line in remove_kernels_file:
  1765.                 remove_kernels.add(line.strip())
  1766.             
  1767.             remove_kernels_file.close()
  1768.         
  1769.         if len(remove_kernels) == 0:
  1770.             self.db.progress('STOP')
  1771.             return None
  1772.         
  1773.         self.db.progress('SET', 1)
  1774.         self.db.progress('REGION', 1, 5)
  1775.         
  1776.         try:
  1777.             self.do_remove(remove_kernels, recursive = True)
  1778.         except:
  1779.             self.db.progress('STOP')
  1780.             raise 
  1781.  
  1782.         self.db.progress('SET', 5)
  1783.         self.db.progress('STOP')
  1784.  
  1785.     
  1786.     def install_extras(self):
  1787.         '''Try to install additional packages requested by installer
  1788.         components.'''
  1789.         sources_list = os.path.join(self.target, 'etc/apt/sources.list')
  1790.         os.rename(sources_list, '%s.apt-setup' % sources_list)
  1791.         old_sources = open('%s.apt-setup' % sources_list)
  1792.         new_sources = open(sources_list, 'w')
  1793.         found_cdrom = False
  1794.         for line in old_sources:
  1795.             if 'cdrom:' in line:
  1796.                 print >>new_sources, line,
  1797.                 found_cdrom = True
  1798.                 continue
  1799.         
  1800.         new_sources.close()
  1801.         old_sources.close()
  1802.         if not found_cdrom:
  1803.             os.rename('%s.apt-setup' % sources_list, sources_list)
  1804.         
  1805.         self.do_install(self.query_recorded_installed())
  1806.         if found_cdrom:
  1807.             os.rename('%s.apt-setup' % sources_list, sources_list)
  1808.         
  1809.         
  1810.         try:
  1811.             if self.db.get('oem-config/enable') == 'true':
  1812.                 if os.path.isdir(os.path.join(self.target, 'home/oem')):
  1813.                     open(os.path.join(self.target, 'home/oem/.hwdb'), 'w').close()
  1814.                     for desktop_file in ('usr/share/applications/oem-config-prepare-gtk.desktop', 'usr/share/applications/kde/oem-config-prepare-kde.desktop'):
  1815.                         if os.path.exists(os.path.join(self.target, desktop_file)):
  1816.                             desktop_base = os.path.basename(desktop_file)
  1817.                             self.chrex('install', '-d', '-o', 'oem', '-g', 'oem', '/home/oem/Desktop')
  1818.                             self.chrex('install', '-o', 'oem', '-g', 'oem', '/%s' % desktop_file, '/home/oem/Desktop/%s' % desktop_base)
  1819.                             break
  1820.                             continue
  1821.                     
  1822.                 
  1823.                 for name in ('gdm-cdd.conf', 'gdm.conf'):
  1824.                     gdm_conf = osextras.realpath_root(self.target, os.path.join('/etc/gdm', name))
  1825.                     if os.path.isfile(gdm_conf):
  1826.                         self.chrex('sed', '-i.oem', '-e', 's/^AutomaticLoginEnable=.*$/AutomaticLoginEnable=true/', '-e', 's/^AutomaticLogin=.*$/AutomaticLogin=oem/', '-e', 's/^TimedLoginEnable=.*$/TimedLoginEnable=true/', '-e', 's/^TimedLogin=.*$/TimedLogin=oem/', '-e', 's/^TimedLoginDelay=.*$/TimedLoginDelay=10/', os.path.join('/etc/gdm', name))
  1827.                         break
  1828.                         continue
  1829.                 
  1830.                 kdmrc = os.path.join(self.target, 'etc/kde4/kdm/kdmrc')
  1831.                 if os.path.isfile(kdmrc):
  1832.                     misc.execute('sed', '-i.oem', '-r', '-e', 's/^#?AutoLoginEnable=.*$/AutoLoginEnable=true/', '-e', 's/^#?AutoLoginUser=.*$/AutoLoginUser=oem/', '-e', 's/^#?AutoReLogin=.*$/AutoReLogin=true/', kdmrc)
  1833.                 
  1834.                 di_locale = self.db.get('debian-installer/locale')
  1835.                 if di_locale:
  1836.                     self.set_debconf('debian-installer/locale', di_locale)
  1837.                 
  1838.         except debconf.DebconfError:
  1839.             pass
  1840.  
  1841.         apt_install_direct = open('/var/lib/ubiquity/apt-install-direct', 'w')
  1842.         apt_install_direct.close()
  1843.  
  1844.     
  1845.     def remove_extras(self):
  1846.         '''Try to remove packages that are needed on the live CD but not on
  1847.         the installed system.'''
  1848.         if os.path.exists('/cdrom/casper/filesystem.manifest-desktop') and os.path.exists('/cdrom/casper/filesystem.manifest'):
  1849.             desktop_packages = set()
  1850.             manifest = open('/cdrom/casper/filesystem.manifest-desktop')
  1851.             for line in manifest:
  1852.                 if line.strip() != '' and not line.startswith('#'):
  1853.                     desktop_packages.add(line.split()[0])
  1854.                     continue
  1855.             
  1856.             manifest.close()
  1857.             live_packages = set()
  1858.             manifest = open('/cdrom/casper/filesystem.manifest')
  1859.             for line in manifest:
  1860.                 if line.strip() != '' and not line.startswith('#'):
  1861.                     live_packages.add(line.split()[0])
  1862.                     continue
  1863.             
  1864.             manifest.close()
  1865.             difference = live_packages - desktop_packages
  1866.         else:
  1867.             difference = set()
  1868.         difference -= self.query_recorded_installed()
  1869.         if len(difference) == 0:
  1870.             return None
  1871.         
  1872.         use_restricted = True
  1873.         
  1874.         try:
  1875.             if self.db.get('apt-setup/restricted') == 'false':
  1876.                 use_restricted = False
  1877.         except debconf.DebconfError:
  1878.             pass
  1879.  
  1880.         if not use_restricted:
  1881.             cache = Cache()
  1882.             for pkg in cache.keys():
  1883.                 if cache[pkg].isInstalled and cache[pkg].section.startswith('restricted/'):
  1884.                     difference.add(pkg)
  1885.                     continue
  1886.             
  1887.             del cache
  1888.         
  1889.         self.do_remove(difference)
  1890.  
  1891.     
  1892.     def remove_broken_cdrom(self):
  1893.         fstab = os.path.join(self.target, 'etc/fstab')
  1894.         ret = []
  1895.         
  1896.         try:
  1897.             fp = open(fstab)
  1898.             for line in fp:
  1899.                 l = line.split()
  1900.                 if len(l) > 2:
  1901.                     if l[1].startswith('/cdrom') or l[1].startswith('/media/cdrom'):
  1902.                         
  1903.                         try:
  1904.                             fstype = subprocess.Popen([
  1905.                                 'vol_id',
  1906.                                 '--type',
  1907.                                 l[0]], stdout = subprocess.PIPE).communicate()[0].rstrip('\n')
  1908.                             if fstype != 'iso9660' and fstype != 'udf':
  1909.                                 continue
  1910.                         except OSError:
  1911.                             pass
  1912.                         except:
  1913.                             None<EXCEPTION MATCH>OSError
  1914.                         
  1915.  
  1916.                     None<EXCEPTION MATCH>OSError
  1917.                 
  1918.                 ret.append(line)
  1919.             
  1920.             fp.close()
  1921.             fp = open(fstab, 'w')
  1922.             fp.writelines(ret)
  1923.         except Exception:
  1924.             e = None
  1925.             syslog.syslog(syslog.LOG_ERR, 'Exception during installation:')
  1926.             syslog.syslog(syslog.LOG_ERR, 'Unable to process /etc/fstab: ' + str(e))
  1927.         finally:
  1928.             if fp:
  1929.                 fp.close()
  1930.             
  1931.  
  1932.  
  1933.     
  1934.     def cleanup(self):
  1935.         '''Miscellaneous cleanup tasks.'''
  1936.         misc.execute('umount', os.path.join(self.target, 'cdrom'))
  1937.         env = dict(os.environ)
  1938.         env['OVERRIDE_BASE_INSTALLABLE'] = '1'
  1939.         subprocess.call([
  1940.             '/usr/lib/ubiquity/apt-setup/finish-install'], env = env)
  1941.         for apt_conf in ('00NoMountCDROM', '00IgnoreTimeConflict', '00AllowUnauthenticated'):
  1942.             
  1943.             try:
  1944.                 os.unlink(os.path.join(self.target, 'etc/apt/apt.conf.d', apt_conf))
  1945.             continue
  1946.             continue
  1947.  
  1948.         
  1949.         if self.source == '/var/lib/ubiquity/source':
  1950.             self.umount_source()
  1951.         
  1952.  
  1953.     
  1954.     def chrex(self, *args):
  1955.         '''executes commands on chroot system (provided by *args).'''
  1956.         return misc.execute('chroot', self.target, *args)
  1957.  
  1958.     
  1959.     def copy_debconf(self, package):
  1960.         '''setting debconf database into installed system.'''
  1961.         targetdb = os.path.join(self.target, 'var/cache/debconf/config.dat')
  1962.         misc.execute('debconf-copydb', 'configdb', 'targetdb', '-p', '^%s/' % package, '--config=Name:targetdb', '--config=Driver:File', '--config=Filename:' + targetdb)
  1963.  
  1964.     
  1965.     def set_debconf(self, question, value):
  1966.         dccomm = subprocess.Popen([
  1967.             'log-output',
  1968.             '-t',
  1969.             'ubiquity',
  1970.             '--pass-stdout',
  1971.             'chroot',
  1972.             self.target,
  1973.             'debconf-communicate',
  1974.             '-fnoninteractive',
  1975.             'ubiquity'], stdin = subprocess.PIPE, stdout = subprocess.PIPE, close_fds = True)
  1976.         
  1977.         try:
  1978.             dc = debconf.Debconf(read = dccomm.stdout, write = dccomm.stdin)
  1979.             dc.set(question, value)
  1980.             dc.fset(question, 'seen', 'true')
  1981.         finally:
  1982.             dccomm.stdin.close()
  1983.             dccomm.wait()
  1984.  
  1985.  
  1986.     
  1987.     def reconfigure_preexec(self):
  1988.         debconf_disconnect()
  1989.         os.environ['XAUTHORITY'] = '/root/.Xauthority'
  1990.  
  1991.     
  1992.     def reconfigure(self, package):
  1993.         '''executes a dpkg-reconfigure into installed system to each
  1994.         package which provided by args.'''
  1995.         subprocess.call([
  1996.             'log-output',
  1997.             '-t',
  1998.             'ubiquity',
  1999.             'chroot',
  2000.             self.target,
  2001.             'dpkg-reconfigure',
  2002.             '-fnoninteractive',
  2003.             package], preexec_fn = self.reconfigure_preexec, close_fds = True)
  2004.  
  2005.  
  2006. if __name__ == '__main__':
  2007.     if not os.path.exists('/var/lib/ubiquity'):
  2008.         os.makedirs('/var/lib/ubiquity')
  2009.     
  2010.     if os.path.exists('/var/lib/ubiquity/install.trace'):
  2011.         os.unlink('/var/lib/ubiquity/install.trace')
  2012.     
  2013.     install = Install()
  2014.     sys.excepthook = install.excepthook
  2015.     install.run()
  2016.     sys.exit(0)
  2017.  
  2018.